package top.jybill.service.Impl;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import top.jybill.dao.IPermissionDao;
import top.jybill.dao.IRoleDao;
import top.jybill.dao.IRolePermissionDao;
import top.jybill.dao.IUserRoleDao;
import top.jybill.domain.MyPageInfo;
import top.jybill.domain.Permission;
import top.jybill.domain.Role;
import top.jybill.domain.RolePermission;
import top.jybill.service.IRoleService;
import top.jybill.utils.CharCodeUtil;

import java.util.List;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class RoleServiceImpl implements IRoleService {

  @Autowired
  private IRoleDao roleDao;
  @Autowired
  private IRolePermissionDao rolePermissionDao;
  @Autowired
  private IPermissionDao permissionDao;
  @Autowired
  private IUserRoleDao userRoleDao;

  /**
   * 根据id查询所有用户
   *
   * @param uId
   * @return
   */
  public List<Role> findRoleById(String uId) {
    List<Role> roles = roleDao.findRoleById(uId);
    if (roles.size() < 1) {
      roles = null;
    }
    return roles;
  }

  /**
   * 查询所有角色
   *
   * @return
   */
  public List<Role> findAllRoles() {
    return roleDao.findAllRoles();
  }

  /**
   * 根据页面信息返回数据
   *
   * @param page
   * @return
   */
  public MyPageInfo findRolesByPage(MyPageInfo page) {
    // 乱码
    System.out.println(page);
    // 模糊查询设置
    if (page.getQuery() == null || page.getQuery().length() < 1 || "null".equals(page.getQuery()) || "".equals(page.getQuery())) {
      page.setQuery(null);
    } else {
      page.setQuery("%" + page.getQuery() + "%");
    }


    // 参数无误
    PageHelper.startPage(page.getPageNum(), page.getPageSize());
    List<Role> roles = roleDao.findRolesByPage(page);
    MyPageInfo retPage = new MyPageInfo(roles);
    retPage.setPageNum(page.getPageNum());
    retPage.setPageSize(page.getPageSize());
    return retPage;
  }

  /**
   * 添加角色并关联
   *
   * @param role
   */
  @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
  public void addRole(Role role) {
    roleDao.addRole(role);

    // 多表关联
    RolePermission rolePermission = new RolePermission();
    rolePermission.setRole(role);
    for (Permission permission : role.getPermissions()) {
      rolePermission.setPermission(permission);
      rolePermissionDao.addRolePermission(rolePermission);
    }
  }

  /**
   * 删除角色
   *
   * @param rid
   */
  @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
  public void delRole(String rid) {
    // 删除角色-权限关联
    rolePermissionDao.delPermissionRoleByrid(rid); // 删除操作

    // 删除用户-角色关联
    userRoleDao.delUserRoleByRid(rid);

    // 删除role
    roleDao.delRole(rid);
  }


  /**
   * 根据角色id查询角色
   *
   * @param rid
   * @return
   */
  public Role findRolesByPage(String rid) {
    return roleDao.findRoleByRid(rid);
  }


  /**
   * 更新角色信息
   *
   * @param role
   */
  @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
  public void updateRole(Role role) {
    roleDao.updateRole(role);
  }

  /**
   * 更新角色 - 权限资源
   *
   * @param role
   */
  @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
  public void updateRolePermissions(Role role) {
    // 根据角色id删除所有  角色 - 权限关系
    rolePermissionDao.delPermissionRoleByrid(role.getId());

    // 根据添加传递过来的所有权限id 建立关系
    RolePermission rolePermission = new RolePermission();
    rolePermission.setRole(role);
    for (Permission permission : role.getPermissions()) {
      rolePermission.setPermission(permission);
      rolePermissionDao.addRolePermission(rolePermission);
    }

  }
}
